package com.inossem.utils.base;


import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class GsonUtils {
   private static Gson g = new Gson();

	public static <T> T copy(T o) {
		String json = g.toJson(o);
		T ord = (T) g.fromJson(json,o.getClass());
		return ord;
	}

	public static <T> T fromJson(String json,Class<T> tClass) {
		T ord = g.fromJson(json,tClass);
		return ord;
	}
	public static String toJson(Object o) {
		String json = g.toJson(o);
		return json;
	}

	public static <T extends Serializable > List<T> copyList(List<T> ls) {
		List<T> nls = new ArrayList<T>();
		for(T obj :ls){
			T t = copy(obj);
			nls.add(t);
		}
		return nls;
	}
	
	
	public static <T,K> List<T> transferList(List<K> ls,Class<T> t){
		List<T> nls = new ArrayList<T>();
		for (K k : ls) {
			String json = g.toJson(k);
			T t1 = (T) g.fromJson(json,t);
			nls.add(t1);
		}
		return nls;
	}
	
	
	public static <T,K> T  transfer(K s,Class<T> t){
		String json = g.toJson(s);
		T t1 = (T) g.fromJson(json,t);
		return t1;
	}
}
